package guyubao.com.controller.admin;

import guyubao.com.GreenLoong.GreenLoong;
import guyubao.com.controller.BaseController;
import guyubao.com.entity.RestBean;
import guyubao.com.entity.dto.JDApplyAccountDto;
import guyubao.com.service.JDAccountService;
import guyubao.com.service.JDApplyAccountService;
import guyubao.com.service.JDCertificateService;
import guyubao.com.service.LoginMapperUserService;
import guyubao.com.utils.SnowflakeIdGenerator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequestMapping("/api/admin/applyJDAccount")
// 开启事务管理
@EnableTransactionManagement
@Tag(name = "申请京东账号相关", description = "申请账号信息的增删改查")
public class AdminJDApplyAccountController extends BaseController{
    @Resource
    JDApplyAccountService applyAccountService;
    @Resource
    JDAccountService jdAccountService;
    @Resource
    JDCertificateService certificateService;
    @Resource
    LoginMapperUserService loginMapperUserService;
    @Resource
    GreenLoong greenLoong;

    @Operation(summary = "管理员身份查询申请的京东账号")
    @GetMapping("/ids")
    public RestBean<List<JDApplyAccountDto>> getJDApplyAccountIds(@RequestAttribute String role){
        if(!isAdmin(role))RestBean.forbidden("您不是管理员");
        return RestBean.success(applyAccountService.getJDApplyAccount());
    }
    // 申请成功 修改state为3
    @Operation(summary = "管理员事申请的京东号成功注册")
    @PutMapping("/idSuccess")
    // 出现问题回滚
    @Transactional
    public RestBean<Void> updateJDApplyAccountIdSuccess(@RequestParam String id,
                                                        @RequestAttribute String role){
        if(!isAdmin(role)) {
            return RestBean.forbidden("您不是管理员");
        }
        applyAccountService.updateJDApplyAccountState(id, 3);
        // 创建雪花iD
        String snowId = String.valueOf(new SnowflakeIdGenerator().nextId());
        // 获得数据
        JDApplyAccountDto account = applyAccountService.getJDApplyAccountById(id);
        // 将指定数据添加入账号jdAccount
        if (jdAccountService.addJDAccount(snowId, account.getPt_pin(), account.getNickname())!=null){
            throw new RuntimeException("添加京东账号出现异常！");
        }
        // 更新指定数据dCertificate
        if (certificateService.updateJDCertificate(snowId,account.getPt_key(),account.getPt_token(),account.getPt_pin())!=null){
            throw new RuntimeException("添加京东账号凭证出现异常！");
        }
        // id与雪花id绑定,loginMapperUser
        if (loginMapperUserService.addLoginMapperUser(id,snowId)!=null){
            throw new RuntimeException("账号绑定出现异常！");
        }
        // 开始将数据推送给青龙
        synchronized (this) {
            String login = greenLoong.login();
            if (login != null) {
                throw new RuntimeException(login);
            }
            String env = greenLoong.getEnv();
            if (env != null) {
                throw new RuntimeException(env);
            }
            String value = "pt_key=" + account.getPt_key()
                    + ";pt_token=" +account.getPt_token() + ";pt_pin=" + account.getPt_pin() + ";";
            String s = greenLoong.addEnv(snowId, value);
            if (s != null) {
                throw new RuntimeException(s);
            }
        }
        return RestBean.success();
    }
    // 申请失败 修改state为2
    @Operation(summary = "管理员事申请的京东号注册失败")
    @PutMapping("/idFail")
    public RestBean<Void> updateJDApplyAccountIdFail(@RequestParam String id,
                                                     @RequestAttribute String role){
        // 如果不是管理员
        if(!isAdmin(role))return RestBean.forbidden("您不是管理员");
        applyAccountService.updateJDApplyAccountState(id, 2);
        return RestBean.success();
    }
}